package com.flanz.apache.velocity.uimocker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.NumberUtils;
import org.apache.commons.lang.StringUtils;

/*
 * StartUpConfig.
 * 
 * @author cf
 */
public class StartUpConfig {
	public final static String PROJECT_DIR = "project_dir";
	public final static String PROJECT_DOCROOT = "docroot_dir";
	public final static String PROJECT_GLOBALMACROS_FILELIST = "globalmacros_filelist";
	public final static String SERVER_PORT = "server_port";
	public final static String MAX_REQUEST_THREADS = "max_request_threads";
	public final static String MIME_MAPPING = "mime_mapping";
	public final static String APP_VARS = "app_vars";
	
	public final static int SERVER_PORT_DEFAULT = 8888;
	private Properties setting;

	StartUpConfig(String fileName) {
		File config = new File(fileName);
		if (!config.exists()) {
			throw new IllegalArgumentException("StartUpConfig not found:"
					+ fileName);
		}
		setting = new Properties();
		try {
			FileInputStream fileInputStream = null;
			fileInputStream = new FileInputStream(config);
			setting.load(fileInputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new IllegalArgumentException("Fail to read:" + fileName);
		} catch (IOException e) {
			e.printStackTrace();
			throw new IllegalArgumentException("Fail to read:" + fileName);
		}
	}

	public String getString(String key, String fallBack) {
		String result = fallBack;
		result = setting.getProperty(key, fallBack);
		return result;
	}

	public String getMandatoryString(String key) {
		String result = getString(key, null);
		if (StringUtils.isBlank(result)) {
			throw new IllegalArgumentException("Setting is mandatory '" + key
					+ "'");
		}
		return result;
	}

	public int getInteger(String key, int fallBack) {
		int result = fallBack;
		String string = getString(key, null);
		if (string != null) {

			result = NumberUtils.createInteger(string);
		}
		return result;

	}
}
